public class LineSegment {
    private int x1, x2;

    public LineSegment(int x1, int x2) {
        this.x1 = x1;
        this.x2 = x2;
    }

    public int getX1() {
        return x1;
    }

    public int getX2() {
        return x2;
    }

    public int getLength() {
        return Math.abs(x2 - x1);
    }

    public boolean intersects(LineSegment other) {
        return !(other.getX1() > this.getX2() || other.getX2() < this.getX1());
    }

    public LineSegment union(LineSegment other) {
        int newX1 = Math.min(this.getX1(), other.getX1());
        int newX2 = Math.max(this.getX2(), other.getX2());
        return new LineSegment(newX1, newX2);
    }
}
